package singlethreaded;
import java.util.Random;
import Controller.main;

public class MonteCarloSimulation 
{
	private double dt;
	private double St;
	private double Sum;
	private double Drift;
	private double vSqrdt;
	private int i;
	private int j;
	private int z;
	

	public MonteCarloSimulation() {
    	this.dt = 0;
    	this.St = 0;
    	this.Sum = 0;
    	this.Drift = 0;
    	this.vSqrdt = 0;
    	this.i = 0;
    	this.j = 0;
    	this.z = 0;
    	
    }
	
	public double MonteCarloStandardOption(String CallPutFlag, double S, double X, double T, double r, double b, double v, int nSteps, int nSimulations) {
		
		long start = System.currentTimeMillis();
		
		this.dt = T / nSteps;
		this.Drift = (b-(v*v)/2)*this.dt;
		this.vSqrdt = v * Math.sqrt(this.dt);
		
		if(CallPutFlag == "c") this.z=1;
		else if(CallPutFlag == "p") this.z=-1;
		
		for(i=0; i<nSimulations; i++)
		{
			CalculSimulation(S, X, nSteps);
			main.window.incrementSingle();
		}
		long Time=System.currentTimeMillis()-start;
		main.window.setTimeSingle(Long.toString(Time));
		
		return Math.exp(-r*T)*(this.Sum/nSimulations);		
	}
	
	void CalculSimulation(double S, double X, int nSteps) {
		
		Random numero = new Random();
		this.St=S;	
		for(j=0; j<nSteps; j++)
		{
			this.St = this.St*Math.exp(this.Drift + this.vSqrdt*numero.nextGaussian());
		}
		this.Sum = this.Sum + Math.max(this.z*(this.St-X), 0);
	}
}
